package com.example.managemyself.util;

import android.content.Context;
import android.util.Log;

import com.example.managemyself.config.AppConfig;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * MySQL数据库连接帮助类
 * 使用AppConfig统一管理数据库连接配置
 */
public class MySQLConnectionHelper {
    private static final String TAG = "MySQLConnectionHelper";
    
    private static MySQLConnectionHelper instance;
    private boolean isInitialized = false;
    private Context applicationContext;
    private AppConfig appConfig;
    
    private MySQLConnectionHelper() {
        appConfig = AppConfig.getInstance();
    }
    
    /**
     * 获取单例实例
     */
    public static synchronized MySQLConnectionHelper getInstance() {
        if (instance == null) {
            instance = new MySQLConnectionHelper();
        }
        return instance;
    }
    
    /**
     * 初始化数据库连接帮助类
     */
    public void initialize(Context context) {
        if (isInitialized) {
            return;
        }
        
        this.applicationContext = context.getApplicationContext();
        
        try {
            // 加载MySQL JDBC驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            isInitialized = true;
            Log.i(TAG, "MySQL连接帮助类初始化成功");
            
            // 打印配置信息（调试模式）
            if (appConfig.isDebugMode()) {
                Log.d(TAG, "数据库连接URL: " + appConfig.getDatabaseUrl());
                Log.d(TAG, "数据库用户名: " + appConfig.getDatabaseUsername());
            }
            
        } catch (ClassNotFoundException e) {
            Log.e(TAG, "MySQL JDBC驱动加载失败", e);
        }
    }
    
    /**
     * 获取数据库连接
     */
    public Connection getConnection() throws SQLException {
        if (!isInitialized) {
            throw new SQLException("MySQLConnectionHelper未初始化，请先调用initialize()方法");
        }
        
        try {
            String url = appConfig.getDatabaseUrl();
            String username = appConfig.getDatabaseUsername();
            String password = appConfig.getDatabasePassword();
            
            Connection connection = DriverManager.getConnection(url, username, password);
            
            if (appConfig.isDebugMode()) {
                Log.d(TAG, "数据库连接成功");
            }
            
            return connection;
            
        } catch (SQLException e) {
            Log.e(TAG, "数据库连接失败: " + e.getMessage(), e);
            throw e;
        }
    }
    
    /**
     * 测试数据库连接
     */
    public boolean testConnection() {
        Connection conn = null;
        try {
            conn = getConnection();
            return conn != null && !conn.isClosed();
        } catch (SQLException e) {
            Log.e(TAG, "数据库连接测试失败", e);
            return false;
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    Log.e(TAG, "关闭测试连接失败", e);
                }
            }
        }
    }
    
    /**
     * 获取连接错误信息
     */
    public String getConnectionErrorMessage() {
        try {
            Connection conn = getConnection();
            if (conn != null) {
                conn.close();
                return "连接成功";
            }
        } catch (SQLException e) {
            return "连接失败: " + e.getMessage();
        } catch (Exception e) {
            return "连接异常: " + e.getMessage();
        }
        return "未知错误";
    }
    
    /**
     * 检查是否已初始化
     */
    public boolean isInitialized() {
        return isInitialized;
    }
    
    /**
     * 获取应用上下文
     */
    public Context getApplicationContext() {
        return applicationContext;
    }
} 